/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi Distribution.

    Maxsi Distribution is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Distribution is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Distribution. If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiDistributionContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiDistributionContributors.h. For more information please see
	MaxsiDistributionContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]

	MaxsiDistribution.dll
	Distributing since 2008

	ThreadInitialize.cpp
	The thread that gets information about the product, before it's installed.

****************************************************************************/

#include "MaxsiEngine.h"
#include "MaxsiDistributionLink.h"
#include "Thread.h"
#include "Data.h"
#include "General.h"

#include <io.h>
#include <stdio.h>
#include <direct.h>
#include <SYS\Stat.h>
#include <fcntl.h>

ME_THREAD_LINK(ThreadInitialize,ClassThreadInitialize,MDLINKCPP);


// TODO: This could be moved to MaxsiEngine.dll, or defined in the ME_THREAD Macro
BOOL ClassThreadInitialize::InitApplication(HANDLE hInstance)
{	
    WNDCLASS  wc = {0};

	BaseClass::InitApplication(hInstance);

    wc.lpfnWndProc = (WNDPROC) ThreadInitializeProc;
    wc.hInstance = (HINSTANCE)hInstance;
    wc.hIcon = 0;
    wc.hCursor = 0;
    wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); 
	wc.lpszMenuName = 0;
	wc.lpszClassName = TEXT("ClassThreadInitialize");

	if (!RegisterClass(&wc)) { return FALSE; }

	return TRUE;
}

BOOL ClassThreadInitialize::InitInstance(HINSTANCE hInstance, INT nCmdShow)
{	
	WindowHWND	=	CreateWindow(
        TEXT("ClassThreadInitialize"),
        TEXT("ClassThreadInitialize"),
		0,
        CW_USEDEFAULT, 0,
        500, 370,
        NULL, NULL, GetModuleHandle(NULL), NULL );

	if ( WindowHWND)
	{
		return TRUE;
	}

	return FALSE;
}

LRESULT APIENTRY ClassThreadInitialize::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    //HDC         hDC         = NULL;
    //PAINTSTRUCT ps          = {0};
    //RECT        rectClient  = {0};

	switch (message) 
    {
        case WM_COMMAND:
		{
			InitializeProduct();
		}
		break;

		default:
		{
			return BaseClass::WindowProc(hWnd,message,wParam,lParam);
		}
    }
    return (0);
}

bool	InitializeProduct()
{
	if (!MasterServer)
	{
		return false;
	}

	//Win7TaskBarSetState(d_ClassThreadInstall->Parent,TBPF_INDETERMINATE);

	char*		Resource		=	GetResourceFromURI(MasterServer);
	char*		Host			=	GetHostFromURI(MasterServer);
	char*		ProductNameEnc	=	EncodeURL(ProductName);

	char*		DownloadURI		=	BuildString(4,Resource,"?action=getproductinformation&product=",ProductNameEnc,"&OutputType=Application/x-MaxsiDataContainer");
	str_replace(DownloadURI," ","+");
	char*		Request			=	BuildString(5,"GET ",DownloadURI," HTTP/1.1\r\nHost: ",Host,"\r\nConnection: Close\r\n\r\n");
			
	HTTPParser_Struct Reply		=	ConnectAndSendHTTPQuery(Request,strlen(Request),Host);

	InputApplicationSlashxDashMaxsiDataContainer(Reply.Out,Reply.OutLenght);

	DeleteHTTPParser_Struct(Reply);

	delete[]	Request;
	delete[]	DownloadURI;
	delete[]	ProductNameEnc;
	delete[]	Host;
	delete[]	Resource;

	if ( IsIngame == false )
	{
		SendMessage(d_ClassThreadInitialize->Parent,WM_COMMAND,GotoSuccessPage,0);
	}

	PostQuitMessage(0);

	
	//Win7TaskBarSetState(d_ClassThreadInstall->Parent,TBPF_NOPROGRESS);


	return	true;
}